<template>
  <div class="my_div">
    <img :src="dog.dogImgUrl" alt="" />
    <p
      :style="{ backgroundColor: bgc || backgroundColor }"
      @click="changebgc, dogshowin(dog.dogName)"
    >
      {{ dog.dogName }}
    </p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      bgc: null,
    };
  },
  props: {
    backgroundColor: {
      type: String,
      default: "#fff",
    },
    dog: {
      type: Object,
      default() {
        return {
          dogImgUrl:
            "https://scpic.chinaz.net/files/pic/pic9/202003/zzpic23514.jpg",
          dogName: "这是一只孤独可怜的狗",
        };
      },
    },
  },
  methods: {
    changebgc() {
      let r = Math.floor(Math.random() * 256);
      let g = Math.floor(Math.random() * 256);
      let b = Math.floor(Math.random() * 256);
      let a = Math.random().toFixed(2);
      let rgba = `rgba(${r},${g},${b},${a})`;

      this.bgc = rgba;
    },
    dogshowin(b) {
      // console.log(b);
      this.$emit("dogshowin", b);
    },
  },
};
</script>

<style scoped>
.my_div {
  width: 200px;
  border: 1px solid black;
  text-align: center;
  float: left;
}

.my_div img {
  width: 100%;
  height: 200px;
}
</style>